package com.usooft.metaTalent.core.base.util;

import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class ResponseUtil {
    public static void download(File file, HttpServletResponse response, String fileName) {
        try {
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();

            response.setContentType("application/octet-stream;charset=UTF-8");
            fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".docx");
            OutputStream ouputStream = response.getOutputStream();
            ouputStream.write(buffer);
            ouputStream.flush();
            ouputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文件下载出现异常", e);
        }
    }
}
